package Tree_09.HuffmanCode_06;

/*
 * @Author 罗
 * @date 2020/3/25 - 12:56 上午
 *
 */

import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class FileZip {
    /**
     * 文件解压
     *
     * @param sourceZipFilePath
     * @param destinyFilePath
     */
    public static void fileDecompression(String sourceZipFilePath, String destinyFilePath) {
        ObjectInputStream in = null;
        OutputStream out = null;
        byte[] huffmanCodeBytes = null;
        Object object = null;
        Map<Byte, String> huffmanCodeTable = null;
        try {
            in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(sourceZipFilePath)));
            out = new BufferedOutputStream(new FileOutputStream(destinyFilePath));
            for (int i = 0; i < 2; i++) {
                object = in.readObject();
                if (object instanceof byte[]) {
                    huffmanCodeBytes = (byte[]) object;
                }
                if (object instanceof Map) {
                    huffmanCodeTable = (Map) object;
                }
            }
            byte[] sourceFileBytes = HuffmanDecode.huffmanDecode(huffmanCodeTable, huffmanCodeBytes);
            out.write(sourceFileBytes);
            out.flush();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }


    /**
     * 文件压缩
     *
     * @param sourceFilePath
     * @param destinyFilePath
     */
    public static void fileZip(String sourceFilePath, String destinyFilePath) {
        byte[] buffer = null;

        int length;
        InputStream in = null;
        OutputStream out = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            in = new BufferedInputStream(new FileInputStream(sourceFilePath));
            out = new BufferedOutputStream(new FileOutputStream(destinyFilePath, false));
            buffer = new byte[in.available()];
            in.read(buffer);

            byte[] huffmanCodeBytes = HuffmanCode.huffmanZip(buffer);
            /*
             * 使用对象流写入文件，而不是普通流
             * */
            objectOutputStream = new ObjectOutputStream(out);
            /*
             * 1.将Huffman编码后的byte[]写入到文件
             * */
            objectOutputStream.writeObject(huffmanCodeBytes);
            objectOutputStream.flush();
            /*
             * 2.将Huffman编码表也一同写入压缩文件
             * */
            objectOutputStream.writeObject(HuffmanCode.getHuffmanCodeTable());
            objectOutputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
